package com.kotlin.lib.coroutines

import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val supervisorJob = SupervisorJob()
    with(CoroutineScope(coroutineContext + supervisorJob)) {

        val firstJob = launch(CoroutineExceptionHandler { coroutineContext, throwable -> }) {
//            delay(200)
            println("The first child is failing")
            throw AssertionError("The first child is cancelled")
        }

        val secondJob = launch {
            firstJob.join()
            println("..firstJob isCancelled ${firstJob.isCancelled}. but second is still active")

            try {
                delay(5000)
            } finally {
                println("..second job finally")
            }
        }

        firstJob.join()
//        delay(300)
        println("cancel supervisor...")

        supervisorJob.cancel()
        secondJob.join()
    }
}